/** Description of AnimateScreen.java 
 * @version 1.0
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.activities.animate;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.panther.awp.android.smoker.R;

public class AnimateScreen extends Activity {
	
	SmokeAnimation animateSmokeView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Make full screen of animation 
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		animateSmokeView = new SmokeAnimation(this);
		setContentView(animateSmokeView);
	}

	@Override
	public void onUserInteraction() {
		super.onUserInteraction();
		moveTaskToBack(false);
		finish();
	}

	private class SmokeAnimation extends View {
		
		private BitmapDrawable simpleSmoke;
		
		private Boolean firstRender;
		private int regeneratedTimes;

		public SmokeAnimation(Context context) {
			super(context);
			simpleSmoke = (BitmapDrawable) getResources().getDrawable(R.drawable.simple_smoke);
			simpleSmoke.setAntiAlias(true);
			firstRender = true;
			regeneratedTimes = 0;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			if(firstRender) {
				simpleSmoke.setBounds(0, canvas.getHeight() - 200, canvas.getWidth(), canvas.getHeight());
				firstRender = false;
			} else {
				moveBoundsAndPixels();
			}
			simpleSmoke.draw(canvas);
			if(!(++regeneratedTimes*10 >= canvas.getHeight())) {
				invalidate();
			} else {
				finish();
				moveTaskToBack(false);
			}
		}

		private void moveBoundsAndPixels() {
			Rect bounds = simpleSmoke.copyBounds();
		    bounds.left -= 0;
		    bounds.right -= 0;
		    bounds.top -= 10;
		    bounds.bottom -= 10;
		    simpleSmoke.setBounds(bounds);
		    simpleSmoke.setLevel(new Random().nextInt(10000));
		}

	}

}
